package com.zhaopin.gitlab;

import com.alibaba.fastjson.JSONObject;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test5 {

    public static void main(String[] args) {

        System.out.println(Math.E*Math.E);

        double log = Math.log(Math.E*Math.E);
        System.out.println(log);
        double a= 31.2624989046325f;

        double b = Math.log10(a + 1) + 1;
        System.out.println(b);

        Stream<String> aa = Stream.of("aa", "bb", "cc", "dd", "dd", "dd", "dd", "aa");
        List<String> viewedJobNumberList = aa.collect(Collectors.toList());

        Map<String,Integer> viewedJobMap = new LinkedHashMap<>();

        for (int i = 0; i < viewedJobNumberList.size(); i++) {
            String jd = viewedJobNumberList.get(i);
            if (viewedJobNumberList.contains(jd)){
                if (viewedJobMap.containsKey(jd)) {
                    viewedJobMap.put(jd, viewedJobMap.get(jd) + 1);
                } else {
                    viewedJobMap.put(jd, 1);
                }
            }
        }

        System.out.println(viewedJobMap);
        //0 当日
        // 其余就是各种其他的
        String str = "2022-10-19T00:00:00.999Z";
        LocalDate now = LocalDate.now();
        System.out.println(now);
        System.out.println(getDaysWithNow(str,now));

        JSONObject jsonObject = new JSONObject();
        for (Map.Entry<String, Object> stringObjectEntry : jsonObject.entrySet()) {

        }

//        features.put("jd_solr_paid_flag","0");
//        features.put("jd_im_p_chats_dealed_per_before_1d","1");
//        features.put("jd_solr_job_update_time","2022-10-08T09:54:07.731Z");
//        features.put("jd_hr_last_active_time","1665384619060");

//        String jd_top_execute_last_time = null;
//        String jd_normal_refresh_times_today = null;
//        String jd_super_expo_refresh_times_today = null;
//        String jd_aduit_pass_time_last = null;
//        String jd_chat_free_coupon_effect_flag = null;
//
//        String jd_chat_free_coupon_effect_last_time = null;
//        String org_vip_level = "1";
//        String jd_solr_paid_flag = "0";
//        String jd_im_talent_chats_per_before_0d = null;
//        String jd_im_search_chats_per_before_0d = null;
//
//        String jd_im_talent_chats_per_before_1d = null;
//        String jd_im_search_chats_per_before_1d = null;
//        String jd_im_p_chats_dealed_in_4h_per_before_0d = null;
//        String jd_im_p_chats_dealed_in_4h_per_before_1d = null;
//        String jd_im_p_chats_dealed_per_before_0d = null;
//
//        String jd_im_p_chats_dealed_per_before_1d = "1";
//        String jd_solr_job_update_time = "2022-10-08T09:54:07.731Z";
//        String jd_hr_last_active_time = "1665384619060";
//        double d = getOperationCoefficient(jd_top_execute_last_time, jd_normal_refresh_times_today, jd_super_expo_refresh_times_today, jd_aduit_pass_time_last, jd_chat_free_coupon_effect_flag, jd_chat_free_coupon_effect_last_time, org_vip_level, jd_solr_paid_flag, jd_im_talent_chats_per_before_0d, jd_im_search_chats_per_before_0d, jd_im_talent_chats_per_before_1d, jd_im_search_chats_per_before_1d, jd_im_p_chats_dealed_in_4h_per_before_0d, jd_im_p_chats_dealed_in_4h_per_before_1d, jd_im_p_chats_dealed_per_before_0d, jd_im_p_chats_dealed_per_before_1d, jd_solr_job_update_time, jd_hr_last_active_time);
//        System.out.println(d);
//        System.out.println(getOperationCoefficient(features));


    }
    static long getDaysWithNow(Object time,LocalDate nowDate) {
        String pattern = "yyyy-MM-dd";
        if (time != null && !time.equals("null") && time instanceof String) {
            String timeStr = time.toString();
            timeStr = timeStr.substring(0, 10);
            LocalDate from = LocalDate.parse(timeStr, DateTimeFormatter.ofPattern(pattern));
            Period between = Period.between(from, nowDate);
            return between.getDays();
        }
        return 9999;
    }

    /**
     * 获取运营系数分
     */
    public static double getOperationCoefficient(Map<String,Object> features) {
        LocalDateTime now = LocalDateTime.now();
        String pattern = "yyyy-MM-dd'T'HH:mm:ss";
        //置顶id
        boolean isUpperId = isAfterNowLong(getOrDefaultLong(features.get("jd_top_execute_last_time")), 1);
        //刷新id
        int refreshId = getOrDefaultInt(features.get("jd_normal_refresh_times_today"));
        //超级曝光id
        int superExpoId = getOrDefaultInt(features.get("jd_super_expo_refresh_times_today"));
        //是否是新客jd
        boolean isNewUserId = isAfterNow(features.get("jd_aduit_pass_time_last"), 2, pattern, now);
        //免体 -特征为1
        int freeEffect = getOrDefaultInt(features.get("jd_chat_free_coupon_effect_flag"));
        // 免体 -生效时间在2日内
        boolean isFreeLastTime = isAfterNowLong(getOrDefaultLong(features.get("jd_chat_free_coupon_effect_last_time")), 2);

        //org_vip_level
        //vip级别 1
        int orgVipLevel = getOrDefaultInt(features.get("org_vip_level"));
        //是否有购买商业化产品
        //jd_solr_paid_flag
        int jdPaidFlag = getOrDefaultInt(features.get("jd_solr_paid_flag"));

        //发起-特征值加和，作为当日发起次数 tanlent
        int jdImTalent0d = getOrDefaultInt(features.get("jd_im_talent_chats_per_before_0d"));

        //发起-特征值加和，作为当日发起次数 ImSearch
        int jdImSearch0d = getOrDefaultInt(features.get("jd_im_search_chats_per_before_0d"));
        //发起-特征值加和，作为前一日发起次数
        int jdImTalent1d = getOrDefaultInt(features.get("jd_im_talent_chats_per_before_1d"));
        //发起-特征值加和，作为前一日发起次数
        int jdImSearch1d = getOrDefaultInt(features.get("jd_im_search_chats_per_before_1d"));

        //回复-特征值加和，作为4h内回复次数
        int jdImPchat4h0d = getOrDefaultInt(features.get("jd_im_p_chats_dealed_in_4h_per_before_0d"));
        //回复-特征值加和，作为4h内回复次数
        int jdImPchat4h1d = getOrDefaultInt(features.get("jd_im_p_chats_dealed_in_4h_per_before_1d"));
        //回复- 特征值加和，作为24h内回复次数
        int jdImChatsDealed0d = getOrDefaultInt(features.get("jd_im_p_chats_dealed_per_before_0d"));
        //回复- 特征值加和，作为24h内回复次数
        int jdImChatsDealed1d = getOrDefaultInt(features.get("jd_im_p_chats_dealed_per_before_1d"));
        //上线
        boolean isReleasTime = isAfterNow(features.get("jd_solr_job_update_time"), 15, pattern, now);

        //活跃
        boolean isHrLastActiveTime = isAfterNowLong(getOrDefaultLong(features.get("jd_hr_last_active_time")), 7);
        //商业化分数计算
        int scoreOfCommericial = 0;
        int refreshSuperExpoId = Math.max(refreshId, superExpoId);
        //商业化分数 ,表示为当天
        if (isUpperId) {
            scoreOfCommericial = scoreOfCommericial + 90;
        } else if (refreshSuperExpoId > 0) {
            scoreOfCommericial = scoreOfCommericial + 60 + 2 * (refreshSuperExpoId - 1);
        } else if (isNewUserId) {
            //新客
            scoreOfCommericial = scoreOfCommericial + 40;
        } else if (freeEffect == 1 && isFreeLastTime) {
            //免体
            scoreOfCommericial = scoreOfCommericial + 40;
        } else if (orgVipLevel == 1 || jdPaidFlag == 0) {
            //付费
            scoreOfCommericial = scoreOfCommericial + 20;
        }

        //勤奋分计算
        double scoreofHardworking = 0;
        // 最近一次账号活跃天数距今 >= 7d and 职位上线时间 >= 15d
        if (getOrDefaultLong(features.get("jd_hr_last_active_time")) != 0 && features.get("jd_solr_job_update_time") != null && !isHrLastActiveTime && !isReleasTime) {
            scoreofHardworking = scoreofHardworking - 50;
        }

        //发起：当日发起次数n，前一日发起次数m，+20*(1-2^(-n))+10*(1-2^(-m))
        scoreofHardworking = scoreofHardworking + 20 * (1 - Math.pow(2.0, -(jdImTalent0d + jdImSearch0d))) + 10 * (1 - Math.pow(2.0, -(jdImTalent1d + jdImSearch1d)));
        //c投递回复：4h内回复次数y，24h内回复次数z，+30*(1-2^(-x))+20*(1-2^(-y))
        scoreofHardworking = scoreofHardworking + 30 * (1 - Math.pow(2.0, -(jdImPchat4h0d + jdImPchat4h1d))) + 20 * (1 - Math.pow(2.0, -(jdImChatsDealed0d + jdImChatsDealed1d)));
        //运营分数
        double operationScore = scoreOfCommericial + scoreofHardworking / 5;
        //运营系数 1/(1 + EXP(1 - 运营分数/20))+0.5
        return 1 / (1 + Math.pow(Math.E, 1 - operationScore / 20)) + 0.5;
    }


    /**
     * 输入的time 增加days时间是否超过当前时间
     * 超过也就是 小于days天 返回true
     *
     * @param time
     * @param days
     * @return
     */
    public static boolean isAfterNowLong(long time, int days) {
        if (time > 0) {
            time = time + days * (24 * 3600 * 1000);
            long l2 = System.currentTimeMillis();
            return time - l2 > 0 ? true : false;
        }
        return false;

    }

    /**
     * 输入的String类型的时间距今长时间 是否超过days ,
     * 如果加2天超过了现在的时刻，意味着之前时间就是2天内的时间 那么值就返回true
     *
     * @param time
     * @param days
     * @param pattern
     * @return
     */
    public static boolean isAfterNow(Object time, int days, String pattern, LocalDateTime now) {
        if (time != null && !time.equals("null") && time instanceof String) {
            String timeStr = time.toString();
            timeStr = timeStr.substring(0, 19);
            LocalDateTime from = LocalDateTime.parse(timeStr, DateTimeFormatter.ofPattern(pattern));
            LocalDateTime localDateTime = from.plusDays(days);
            return localDateTime.isAfter(now);
        }

        return false;

    }

    public static long getOrDefaultLong(Object cell) {
        if (null == cell) {
            return 0L;
        } else if (cell.equals("null")) {
            return 0L;
        } else if(cell instanceof String){
            return Long.parseLong(cell.toString());
        }else{
            return 0L;
        }

    }

    public static int getOrDefaultInt(Object cell) {
        if (null == cell) {
            return 0;
        } else if (cell.equals("null")) {
            return 0;
        } else if(cell instanceof String){
            return Integer.parseInt(cell.toString());
        }else {
            return 0;
        }
    }

}
